// pages/shopping/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product: [],
    allProduct: [],
    cart: [],
    allCart: [],
    HZL_num: 0,
    // 搜索框
    inputViewWidth: "100%",
    srchInputValue:"",
    srchInputFocus:false,
    delBtnDisplay: "none",
    resetBtnDisplay: "none",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var token = wx.getStorageSync('token');
    wx.request({
      url: 'http://127.0.0.1:8080/system/product/list', // 获取所有商品接口
      method: 'get',
      header: {
        'content-type': 'application/json',  //这里注意POST请求content-type是小写，大写会报错  
        'Authorization': token
      },
      success(res) {
        if(res.data.code==200) {
          var productList = res.data.rows
          for(var i=0;i<productList.length;i++){
            productList[i]['num'] = 0
          }
          that.setData({
            product: productList
          })
        }
      }
    })
  },

  /* 搜索功能 */
  // 点击搜索框
  clickSearchInput:function(e){
    var that = this;
    var product = this.data.product;
    if(that.data.allProduct.length == 0){
      that.setData({
        allProduct: product
      })
    }
    // 当搜索值更改后，强行赋值
    if(that.data.allProduct.length == product.length){
      that.setData({
        allProduct: product
      })
    } else {
      var allProduct = that.data.allProduct;
      for(var i=0; i<product.length; i++){
        for(var j=0; j<allProduct.length; j++){
          if(allProduct[j].id == product[i].id){
            allProduct[j] = product[i]
          }
        }
      }
      that.setData({
        allProduct: allProduct
      })
    }
  },
  // 搜索框输入监听
  srchInputChange:function(e){
    var that = this;
    var inputValue = e.detail.value;
    var product = that.data.product;
    var localProduct = [];
    that.setData({
      srchInputValue: inputValue
    });
    
    // 当搜索值更改后，强行赋值
    if(that.data.allProduct.length == product.length){
      that.setData({
        allProduct: product
      })
    } else {
      var allProduct = that.data.allProduct;
      for(var i=0; i<product.length; i++){
        for(var j=0; j<allProduct.length; j++){
          if(allProduct[j].id == product[i].id){
            allProduct[j] = product[i]
          }
        }
      }
      that.setData({
        allProduct: allProduct
      })
    }

    if (inputValue!=undefined&&inputValue.length>0){
      for(var i=0; i< product.length; i++){
        if(product[i].name.indexOf(inputValue)>=0){
          localProduct.push(product[i])
        }
      }
      that.setData({
        product: localProduct
      })
    } else {
      that.setData({
        product: that.data.allProduct
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  
  /**
   * 增加数量按钮
   */
  HZL_jia:function(e)
  {
    var that = this;
    var product = e.currentTarget.dataset.data;
    product.num = product.num+1;
    var products = that.data.product;
    var allProduct = that.data.allProduct;
    var cart = [];
    var HZL_num = 0;
    for(var i=0; i< products.length; i++){
      if(products[i].id == product.id){
        products[i] = product;
      }
    }

    // 主要计算购物车和购物数量
    if(allProduct.length == 0){
        allProduct = that.data.product
    }
    for(var i=0; i< allProduct.length; i++){
      if(allProduct[i].id == product.id){
        allProduct[i] = product;
      }
    }
    for(var i=0; i<allProduct.length; i++){
      if(allProduct[i].num != 0){
        cart.push(allProduct[i])
        HZL_num = HZL_num + allProduct[i].num
      }
    }
    that.setData({
      product: products,
      cart: cart,
      HZL_num: HZL_num
    })
  },
  
  /**
   * 减少数量按钮
   */
  HZL_jian:function(e)
  {
    var that = this;
    var product = e.currentTarget.dataset.data;
    product.num = product.num-1;
    var products = that.data.product;
    var allProduct = that.data.allProduct;
    var cart = [];
    var HZL_num = 0;
    for(var i=0; i< products.length; i++){
      if(products[i].id == product.id){
        products[i] = product;
      }
    }

    // 主要计算购物车和购物数量
    for(var i=0; i< allProduct.length; i++){
      if(allProduct[i].id == product.id){
        allProduct[i] = product;
      }
    }
    for(var i=0; i<allProduct.length; i++){
      if(allProduct[i].num != 0){
        cart.push(allProduct[i])
        HZL_num = HZL_num + allProduct[i].num
      }
    }
    that.setData({
      product: products,
      cart: cart,
      HZL_num: HZL_num
    })
  },

  /**
   * 去结算按钮
   * 具体功能自己写
   */
  go: function()
  {
    // 用户购物车(包含选择的商品)
    var myCat = this.data.cart
    if(myCat!=''){
      wx.setStorageSync('myCat', myCat)
      wx.reLaunch({
        url: "/pages/addOrder/index",
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})